let dataInfo = [{
    name: "安东尼 戴维斯",
    money: 892001
  },
  {
    name: "凯里 欧文",
    money: 668932
  },
  {
    name: "史蒂夫 库里",
    money: 698756
  },
  {
    name: "德里克 罗斯",
    money: 492489
  },
  {
    name: "保罗 乔治",
    money: 853957
  },
  {
    name: "勒布朗 詹姆斯",
    money: 968472
  },
  {
    name: "詹姆斯 哈登",
    money: 718225
  },
  {
    name: "凯文 杜兰特",
    money: 889147
  },
];

let dataLength=dataInfo.length,
    newData=[],
    toggle = document.getElementById("toggle"),
    container = document.querySelector(".container"),
    main = container.querySelector("main"),
    p = main.querySelectorAll("p"),
    money=[],
    sumMoney=0,//总额
    dbMoney = [];//资金翻倍

    
// show-nav
let flag = true;
toggle.onclick=()=>{
  if(flag){
    document.querySelector("nav").style.transform="translate(0)";
    document.querySelector("header").classList.add("show_nav");
    container.classList.add("show_nav");
  }else{
    document.querySelector("nav").style.transform="translate(-100%)";
    document.querySelector("header").classList.remove("show_nav");
    container.classList.remove("show_nav");
  }
  flag = !flag;
};

container.addEventListener("click",e=>{
    (e.target.id == "add-user" )&& addUser();
    (e.target.id == "double" )&& double();
    (e.target.id == "show-millionaire" )&& showMillionaire();
    (e.target.id == "calculate" )&& calculate();
    (e.target.id == "sort" )&& sort();
});

let newDiv = document.createElement("div");
    main.appendChild(newDiv);
    
// addUser
let i=0;//计数变量
function addUser(){
  if(i<=dataLength){
    newData[i]=dataInfo[i];
    money.push(dataInfo[i].money/10000);
    newD(dataInfo[i].name,money[i])
    // calculate
    sumMoney+=money[i];
    i++;
  }
};
// double
function double(){
  main.querySelectorAll("span:last-Child>span").forEach((val,idx)=>{
    money[idx]=money[idx]*2;
    console.log( money[idx]);
    val.innerHTML=`${money[idx]}`;
    sumMoney*=2;  
  });
};
// show-millionaire
function showMillionaire(){
  newDiv.innerHTML="";
  money.forEach((val,idx)=>(val>100)&& newD(dataInfo[idx].name,val));
}
// sort
function sort(){
    newDiv.innerHTML="";
    newData.sort((a,b)=>(b.money-a.money)).forEach(val=>newD(val.name,val.money/10000))
}
// calculate
function calculate(){
  let newSpan = document.createElement("span");
  container.style.position="relative";
  newSpan.style.cssText="font-size: 18px;position: absolute;bottom:0;right:0";
  newSpan.innerHTML=`Total:${sumMoney}万`;
  main.appendChild(newSpan);
}

//封装函数 newDiv
function newD(Name,Money){
  let newP = document.createElement("p");
  newP.classList.add("person");
  newP.innerHTML = `<span>${Name}</span><span><span id="money">${Money}</span>万</span>`;
  newDiv.appendChild(newP);
}